package com.woniuxy.service.Impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.woniuxy.dao.ApplyDao;
import com.woniuxy.entity.LoanApply;
import com.woniuxy.service.ApplyService;
import com.woniuxy.util.ContractUtil;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @description: 申请贷款的具体业务层 （ ApplyService）
 * @author quanquan
 * @date 2022/9/20 11:53
 * @version 1.0
 */

@Service
@MapperScan("com.woniuxy.dao")
public class ApplyServiceImpl implements ApplyService {
    @Autowired(required = false)
    ApplyDao applyDao;

    /**
     * 添加信息
     * @param loanApply
     * @return
     */
    @Override
    public AjaxResult addApply(LoanApply loanApply) {
        //放入申请用户id
        loanApply.setUserId(Math.toIntExact(SecurityUtils.getUserId()));
        //申请时间
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        loanApply.setApplicationDate(formatter.format(date));
        //生成合同编号
        loanApply.setContractId(ContractUtil.getContractNum(Math.toIntExact(SecurityUtils.getUserId())));
        int i = applyDao.addApply(loanApply);
        if (i>0){
            return new AjaxResult(200,"成功");
        }else {
            return new AjaxResult(500,"失败");
        }

    }
}
